// PROGRAMA : SCRPP002
// SISTEMA : SISP - SISTEMA DE INFORMACOES DE SEGURANCA PUBLICA
// IDENTIFICACAO CRIMINAL
// BIBLIOTECA : SISP
// CLIENTE : GEJUSPC - GERENCIA DE ESTADO DA JUSTICA, SEGURANCA
// PUBLICA E CIDADANIA / INSTITUTO DE IDENT.
// OBJETIVO : EMITE ATESTADO DE ANTECEDENTES
// PROGRAMADOR: EDNA PINTO / BRUNO LOPES
// DATA : 03 DE FEVEREIRO DE 1998
// ALTERACAO : 26/08/1998 POR SOLON MIRANDA
// : 12/08/2002 POR SOLON MIRANDA
package br.com.vti.sisp_sicrim.antigo.controller.programa;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;

import net.sf.jasperreports.engine.JRException;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.interceptor.download.InputStreamDownload;
import br.com.vti.sisp_sicrim.antigo.controller.relatorios.Scrpp002Report;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Idctea05;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Idctea06;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Idctea07;
import br.com.vti.sisp_sicrim.antigo.controller.tela.Scrtc001;
import br.com.vti.sisp_sicrim.antigo.model.DidCadastro;
import br.com.vti.sisp_sicrim.antigo.model.DidEmissao;
import br.com.vti.sisp_sicrim.antigo.model.Estla001;
import br.com.vti.sisp_sicrim.antigo.model.SicCadastro;
import br.com.vti.sisp_sicrim.antigo.model.SicCadnovo;
import br.com.vti.sisp_sicrim.antigo.model.SicControle;
import br.com.vti.sisp_sicrim.antigo.model.SicEstatistica;
import br.com.vti.sisp_sicrim.antigo.model.SicSenha;
import br.com.vti.sisp_sicrim.antigo.model.SicTabelas;
import br.com.vti.sisp_sicrim.antigo.model.Sislac01;
import br.com.vti.sisp_sicrim.antigo.util.ExceptionProgram;
import br.com.vti.sisp_sicrim.antigo.util.Scrg001;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;

/**
 * Classe : Scrpp002 OBJETIVO : EMITE ATESTADO DE ANTECEDENTES *
 * 
 * @author Incode - Data: mai/2014
 * 
 */
@Resource
@Path("/antigo/scrpp002")
public class Scrpp002 {
	// Views utilizadas
	SicSenha senhaView = new SicSenha();
	DidCadastro vwDidcad = new DidCadastro();
	DidEmissao vwEmissao = new DidEmissao();
	SicCadastro criminalView = new SicCadastro();
	SicCadnovo sicCadnovoView = new SicCadnovo();
	SicTabelas vwTabele = new SicTabelas();
	SicControle vwDvecon = new SicControle();
	SicTabelas vwDvetab = new SicTabelas();
	SicEstatistica vwCadest = new SicEstatistica();
	// Telas/relatorios utilizados
	Idctea05 idctea05;
	Idctea06 idctea06;
	Idctea07 idctea07;
	Scrtc001 scrtc001;
	Scrpp002Report report;
	// Includes utilizados
	Estla001 estla001 = new Estla001();
	Sislac01 sislac01 = new Sislac01();
	// Areas globais / session
	Scrg001 scrg001;
	// Funcoes/subrotinas
	FuncoesGerais funcoes = new FuncoesGerais();

	Result result;
	Sessao sessao;
	Boolean primeiraVez = false;
	Scrpp002 scrpp002Anterior;
	String faseproc = "0";

	private String chassi = "";
	private String editRg = "";
	private String jNomeMae = "";
	private String jNomePai = "";
	private int data8 = 0;
	private String[] comun = new String[3];
	private String codmun = "";
	private String filia2 = "";
	private String chave = "";
	private int jDtNasc = 0;
	private int horatemp = 0;
	private int jCont = 0;
	private String jNsenh = "";
	private String datatual = "";
	// VARIAVEIS PARA ESTATISTICA
	// NOME DO IDENTIFICADO
	private String motida = "";
	private String[] tabm = { "", "   ", "VEI", "CON", "MUL", "   ", "IPV", "ROU", "   ", "   ", "   ", "   ", "   ", "IDC", "ICR", "FUN", "   ", "   " };
	private String chalfa1 = "";
	private String descel1 = "";
	// CONTEM
	private String mensagemInicial = "";

	private Idctea05 preMapas() {
		int ind;
		sessao.getScrg001().setWVltw(04);
		sessao.getScrg001().setWProgramaw(this.getClass().getSimpleName().toUpperCase());

		idctea05 = new Idctea05(true, sessao);
		idctea06 = new Idctea06(true, sessao);
		idctea07 = new Idctea07(true, sessao);
		scrtc001 = new Scrtc001(true, sessao);
		Arrays.fill(comun, "");
		// Arrays.fill(nomide, "");
		for (ind = 1; ind <= 3; ind++) {
			scrtc001.setJuseropc(ind, "");
		}
		// idctea06.setDataCorrente(UtilBean.inverteDataCorrente());
		// idctea07.setDataCorrente(UtilBean.inverteDataCorrente());

		idctea05.setJregger(0);
		idctea05.setConf("");
		estla001.cpo = 7;
		estla001.esttip = 99;
		data8 = UtilBean.obtemDataCorrente();

		return idctea05;
	}

	public Scrpp002(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	@Post
	public InputStreamDownload executar(Idctea05 idctea05, Idctea06 idctea06, Idctea07 idctea07, Scrtc001 scrtc001) {
		try {
			return main(idctea05, idctea06, idctea07, scrtc001);
		} catch (Exception e) {
			ExceptionProgram excp = new ExceptionProgram();
			excp.trataErroScrcerro(e, sessao, this.getClass().getSimpleName());
		}
		return null;
	}

	private InputStreamDownload main(Idctea05 pidctea05, Idctea06 pidctea06, Idctea07 pidctea07, Scrtc001 pscrtc001) throws Exception {
		if (pidctea05 == null && pidctea06 == null && pidctea07 == null && pscrtc001 == null) {
			primeiraVez = true;
			// idctea05 = new Idctea05 (primeiraVez);
			idctea05 = preMapas();
			idctea05.setNavegar("../scrpp002/executar");
			idctea05.setMensagem(mensagemInicial);
			result.include("idctea05", idctea05);
			sessao.setAtributo("SCRPP002", this);
			idctea05.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return null;
		} else {
			restaurarEstados(pidctea05, pidctea06, pidctea07, pscrtc001);
		}

		idctea05.receberTela(false, result);
		if (idctea05.temMensagem) {
			result.include("idctea05", idctea05);
			sessao.setAtributo("SCRPP002", this);
			idctea05.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return null;
		}
		if (pidctea06 != null && UtilBean.ctt_retorna.equals(pidctea06.getFuncao())) {
			Scrpp002 scrpp002 = new Scrpp002(result, sessao);
			scrpp002.executar(null, null, null, null);
			return null;
		}
		if (UtilBean.funcao.equals(UtilBean.ctt_retorna)) {
			Scrpp001 scrpp001 = new Scrpp001(result, sessao);
			scrpp001.executar(null);
			return null;
		}

		// Par�grafo: fd1()
		senhaView.consultaMatricula(idctea05.getJmatriculaj());
		jCont = jCont + 1;
		if (jCont == 3) {
			Scrpp001 scrpp001 = new Scrpp001(result, sessao);
			scrpp001.setMensagem("Voce ultrapassou o numero	maximo de tentativas - Tecla ENTER");
			scrpp001.executar(null);
			return null;
		}
		if (!idctea05.getJsenhaj().trim().equals(senhaView.getSenha().trim())) {
			idctea05.enviarMensagem("jSenhaj", "VERIFIQUE CORRETAMENTE SUA SENHA");
			result.include("idctea05", idctea05);
			sessao.setAtributo("SCRPP002", this);
			idctea05.enviarTela(primeiraVez, result, getClass().getSimpleName().toUpperCase());
			return null;
		}
		sessao.getScrg001().setWNivelw(senhaView.getNivel());
		if (!jNsenh.trim().equals("")) {
			idctea05.setMensagem("Informe nova senha p/ confirmacao             	");
			if (jNsenh != idctea05.getJWsenh()) {
				idctea05.setMensagem("Confirmacao invalida - Tecla ENTER");
				// SCRPP002
				Scrpp001 scrpp001 = new Scrpp001(result, sessao);
				scrpp001.executar(null);
				return null;
			} else {
				senhaView.setSenha(idctea05.getJWsenh());
				senhaView.atualiza();
				// dao.commit();
				sessao.getScrg001().setWSenhaw(idctea05.getJWsenh());
			}
		}
		if (senhaView.registrosProcessados() == 0) {
			Scrpp001 scrpp001 = new Scrpp001(result, sessao);
			scrpp001.setMensagem("Usuario nao cadastrado no sistema");
			scrpp001.executar(null);
			return null;
		}
		// dao.rollback();
		if (!UtilBean.funcao.equals(UtilBean.ctt_processa)) {
			idctea05.enviarMensagem(null, "");
			return null;
		}

		// DEFINE PRINTER(1)
		// RELATORIO 1 PS=33 LS=80 SG=OFF
		report = new Scrpp002Report();
		report.setPageSize(33);
		report.setLineSize(80);
		if (!UtilBean.funcao.equals(UtilBean.ctt_processa)) {
			idctea05.enviarMensagem(null, "");
			return null;
		}

		// TABELA DE USER-ID/POSTO
		while (vwDvetab.pesquisaTabele(888, (long) sessao.getScrg001().getWMatriculaw())) {
			descel1 = vwDvetab.getDescricaoElem();
			chalfa1 = vwDvetab.getChaveAlfaAcesso();
		}
		if (vwDvetab.registrosProcessados() == 0) {
			// UtilBean.salvaAcao("br.com.vti.sisp.mb.programa.Scrpp002"); // STACK
			Scrpp002 scrpp002 = new Scrpp002(result, sessao);
			scrpp002.setMensagem("USUARIO NAO AUTORIZADO PARA EMITIR O ATESTADO " + sessao.getScrg001().getWMatriculaw());
			scrpp002.executar(null, null, null, null);
			return null;
		}

		if (idctea05.getConf().trim().equals("") || UtilBean.funcao.equals(UtilBean.ctt_processa)) {
			chave = "";
			if (idctea05.getJregger() != 0) {
				while (vwDvecon.pesquisaNumeroDocumentoTipoDocumentoIn(idctea05.getJregger(), 87, 88)) {
					if (vwDvecon.getTipoDocumento() == 87) {
						Scrpp002 scrpp002 = new Scrpp002(result, sessao);
						scrpp002.setMensagem("DOCUMENTO CANCELADO EMISSAO NAO PERMITIDA");
						scrpp002.executar(null, null, null, null);
						return null;
						// REINPUT 'DOCUMENTO CANCELADO EMISSAO NAO PERMITIDA'
						// MARK *#REGGER ALARM
					}
				}
				chassi = "";
				if (!acha(pidctea07, pidctea06, pscrtc001)) {
					return null;
				}
				// CALLNAT 'ESTNA001' CPO ESTTIP QDE #VLT# #MATRICULA
				// BACKOUT TRANSACTION
				estatistica();
			} else {
				if (pesquisaCadastroNovo(pidctea07, pidctea06, pscrtc001)) {
					vwDidcad.setNumeroPosto((UtilBean.subStr(descel1, 38, 3).trim().equals("")) ? 0 : Integer.parseInt(UtilBean.subStr(descel1, 38, 3)));
					sicCadnovoView.setNumeroPosto((UtilBean.subStr(descel1, 38, 3).trim().equals("")) ? 0 : Integer.parseInt(UtilBean.subStr(descel1, 38, 3)));
					estatistica();
					gravaDidEmissao();
				} else {
					return null;
				}
			}
		} else {
			pesquisaCadastroNovo(pidctea07, pidctea06, pscrtc001);
			vwDidcad.setNumeroPosto((UtilBean.subStr(descel1, 38, 3).trim().equals("")) ? 0 : Integer.parseInt(UtilBean.subStr(descel1, 38, 3)));
			sicCadnovoView.setNumeroPosto((UtilBean.subStr(descel1, 38, 3).trim().equals("")) ? 0 : Integer.parseInt(UtilBean.subStr(descel1, 38, 3)));
			estatistica();
			gravaDidEmissao();
		}

		// UtilBean.salvaAcao("br.com.vti.sisp.mb.programa.Scrpp002"); // STACK
		return report.imprimir();
		// FIM
	}

	private boolean acha(Idctea07 pidctea07, Idctea06 pidctea06, Scrtc001 pscrtc001) throws Exception {
		if (vwDidcad.consultaNumeroRg(idctea05.getJregger())) {
			report.setNomide(1, vwDidcad.getNomeIdent());
			jNomePai = vwDidcad.getNomePai();
			jNomeMae = vwDidcad.getNomeMae();
			report.setF1(vwDidcad.getNomePai());
			report.setF2(vwDidcad.getNomeMae());
			if (jNomeMae.trim().equals("")) {
				jNomeMae = "************";
			}
			if (jNomePai.trim().equals("")) {
				jNomePai = "************";
			}
			chave = " ";
			criminal();
			if (chave.equals("N"))
			// REINPUT 'ATENCAO. PESSOA COM ALGUMA RESTRICAO.'
			// MARK *#REGGER ALARM
			// REINPUT 'FAVOR CONSULTAR, ANTES DA ENTREGA DO ATESTADO'
			{
				return false;
			}
			// MARK *#REGGER ALARM
			report.setFilia1(UtilBean.moveSubstring(report.getFilia1(), 118, 3, "A", " E "));
			report.setDatnas(vwDidcad.getDataNasc());
			report.setDatnas((int) funcoes.sissd002(report.getDatnas()));
			vwDvetab.pesq = UtilBean.moveSubstring(vwDvetab.pesq, 0, 3, "N", 1);
			vwDvetab.pesq = UtilBean.moveSubstring(vwDvetab.pesq, 3, 12, "N", vwDidcad.getNomeMunicipioIdent());
			vwDvetab.resp = funcoes.sisst001(1, vwDidcad.getNomeMunicipioIdent(), vwDvetab.resp);
			codmun = vwTabele.resp;
			comun[1] = UtilBean.concatena(codmun, "-", vwDidcad.getUfMunicipioNasc());
			chassi = UtilBean.formataMascara(idctea06.getJregger(), "N", 13, "ZZZZZZZZZ999-9");
			// RELATORIO 1 PS=33 LS=80 SG=OFF
			report.setPageSize(33);
			report.setLineSize(80);
			// RELATORIO
			report.setWnumreg("");
			editRg = "";
			editRg = UtilBean.moveSubstring(editRg, 0, 12, "A", UtilBean.extraiDigitos(idctea05.getJregger(), 13, 1, 12));
			editRg = UtilBean.moveSubstring(editRg, 12, 1, "A", UtilBean.extraiDigitos(idctea05.getJregger(), 13, 13, 1));
			report.setWnumreg(String.format("%12s-%s/SSP-MA", UtilBean.subStr(editRg, 0, 12), UtilBean.subStr(editRg, 12, 1)));
			report.setFx(UtilBean.concatena(report.getF1(), " E"));
			datatual = String.valueOf(UtilBean.obtemDataCorrente());
			report.setDataprint(UtilBean.subStr(datatual, 6, 2) + "/" + UtilBean.subStr(datatual, 4, 2) + "/" + UtilBean.subStr(datatual, 0, 4));
			report.gravaRelatorio(1);

			// RELATORIO
			gravaDidEmissao();
			report.setJregger(0);
		}
		if (vwDidcad.totalRegistros() == 0) {
			return pesquisaCadastroNovo(pidctea07, pidctea06, pscrtc001);
			// UtilBean.salvaAcao("br.com.vti.sisp.mb.programa.Scrpp002"); // STACK
		}

		return true;
	}

	private boolean cadastraNovo(Idctea06 pidctea06) throws Exception {

		if (pidctea06 == null) {
			result.include("idctea06", idctea06);
			sessao.setAtributo("SCRPP002", this);
			idctea06.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			return false;
		} else {
			idctea06.receberTela(false, result);
			if (idctea06.temMensagem) {
				result.include("idctea06", idctea06);
				sessao.setAtributo("SCRPP002", this);
				if (!result.used()) {
					idctea06.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
				}
				return false;
			}
		}
		sicCadnovoView.setTipoRg("ET");
		sicCadnovoView.setNumeroRg(idctea06.getJregger());
		sicCadnovoView.setNomePessoa(idctea06.getJdadnome());
		sicCadnovoView.setNomePai(idctea06.getJdadpai());
		sicCadnovoView.setNomeMae(idctea06.getJdadmae());
		sicCadnovoView.setDataReg(UtilBean.obtemDataCorrente());
		jDtNasc = idctea06.getJdadano() * 10000 + idctea06.getJdadmes() * 100 + idctea06.getJdaddia();
		sicCadnovoView.setDataNasc(jDtNasc);
		sicCadnovoView.setMatriculaFuncAtu(idctea06.getJmatriculaj());
		sicCadnovoView.setHoraAtu(Integer.parseInt((new SimpleDateFormat("HHmmss").format(Calendar.getInstance().getTime()))));
		sicCadnovoView.setUf(idctea06.getJdadpaisorigem());
		int tPosto = (UtilBean.subStr(descel1, 38, 3).trim().equals("")) ? 0 : Integer.parseInt(UtilBean.subStr(descel1, 38, 3));
		sicCadnovoView.setNumeroPosto(tPosto);
		if (!idctea06.getJdadnumpassaporte().trim().equals("")) {
			sicCadnovoView.setPassaporte(idctea06.getJdadnumpassaporte());
		}
		impAtestaEstrang(idctea06.getJdadestrangsn());
		sicCadnovoView.grava();
		// dao.commit();
		gravaDidEmissao();
		return true;
	}

	private void criminal() {
		int ind = 0;
		while (criminalView.pesquisaNomeCompletoNomeMae(report.getNomide(1), jNomeMae)) {
			// 696
			if (criminalView.getStatusReg() != 1)
			// REGISTRO-FEDERAL-CI = #REGGER
			{
				for (ind = 1; ind <= 150; ind++) {
					if (!criminalView.getObservacao(ind).trim().equals("")) {
						chave = "N";
					}
				}
			}

		}
	}

	private void impAtestaEstrang(String estrangeiroSn) throws Exception {
		report.setNomide(1, sicCadnovoView.getNomePessoa());
		jNomePai = sicCadnovoView.getNomePai();
		jNomeMae = sicCadnovoView.getNomeMae();
		report.setF1(sicCadnovoView.getNomePai());
		report.setF2(sicCadnovoView.getNomeMae());
		if (jNomeMae.trim().equals("")) {
			jNomeMae = "************";
		}
		if (jNomePai.trim().equals("")) {
			jNomePai = "************";
		}

		// report.setFilia1(UtilBean.moveSubstring(report.getFilia1(), 118, 3, "A", " E "));
		report.setDatnas(sicCadnovoView.getDataNasc());
		report.setDatnas((int) funcoes.sissd002(report.getDatnas()));
		report.setNovoRegger(sicCadnovoView.getNumeroRg());
		report.setNovoTipoReg(sicCadnovoView.getTipoRg());
		vwDvetab.pesq = UtilBean.moveSubstring(vwDvetab.pesq, 0, 3, "N", 1);
		vwDvetab.pesq = UtilBean.moveSubstring(vwDvetab.pesq, 3, 12, "N", vwDidcad.getNomeMunicipioIdent());
		vwDvetab.resp = funcoes.sisst001(1, vwDidcad.getNomeMunicipioIdent(), vwDvetab.resp);
		codmun = vwTabele.resp;
		comun[1] = UtilBean.concatena(codmun, "-", vwDidcad.getUfMunicipioNasc());
		// RELATORIO 1 PS=33 LS=80 SG=OFF
		report.setPageSize(33);
		report.setLineSize(80);
		// RELATORIO
		report.setWnumreg("");
		editRg = "";
		editRg = UtilBean.moveSubstring(editRg, 0, 12, "A", sicCadnovoView.getNumeroRg());
		editRg = UtilBean.moveSubstring(editRg, 12, 1, "A", sicCadnovoView.getNumeroRg());

		if (estrangeiroSn.equals("N") || estrangeiroSn.trim().equals("")) {
			report.setWnumreg(String.format("%12s%s/%s", UtilBean.subStr(editRg, 0, 12), UtilBean.subStr(editRg, 12, 1), sicCadnovoView.getUf()));
			report.setFx(UtilBean.concatena(report.getF1(), " E"));
			datatual = String.valueOf(UtilBean.obtemDataCorrente());
			report.setDataprint(UtilBean.subStr(datatual, 6, 2) + "/" + UtilBean.subStr(datatual, 4, 2) + "/" + UtilBean.subStr(datatual, 0, 4));
			report.gravaRelatorio(2);
			// RELATORIO
		} else {
			report.setJdadnumpassaporte(sicCadnovoView.getPassaporte());
			report.setJdadpaisorigem(sicCadnovoView.getUf());
			datatual = String.valueOf(UtilBean.obtemDataCorrente());
			report.setDataprint(UtilBean.subStr(datatual, 6, 2) + "/" + UtilBean.subStr(datatual, 4, 2) + "/" + UtilBean.subStr(datatual, 0, 4));
			report.gravaRelatorio(3);
			// RELATORIO
		}
		gravaDidEmissao();
		// report.setJregger(0);
	}

	private boolean pesquisaCadastroNovo(Idctea07 pidctea07, Idctea06 pidctea06, Scrtc001 pscrtc001) throws Exception {
		// Par�grafo: fdr2()
		if (sicCadnovoView.consultaNumeroRg(idctea05.getJregger())) {
			if (pscrtc001 == null && pidctea07 == null) {
				result.include("scrtc001", scrtc001);
				sessao.setAtributo("SCRPP002", this);
				scrtc001.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
				return false;
			} else {
				scrtc001.receberTela(false, result);
			}
			// Analisa condi��es

			if (scrtc001.getJuseropc(1).equals("X")) {
				transfereDadosParaTela();
				impAtestaEstrang(idctea06.getJdadestrangsn());
			} else if (scrtc001.getJuseropc(2).equals("X")) {
				return alteraCadastroNovo(pidctea07);
			} else if (scrtc001.getJuseropc(3).equals("X")) {
				return excluiCadastroNovo(pidctea07);
			} else {
				transfereDadosParaTela();
				impAtestaEstrang(idctea06.getJdadestrangsn());
			}

			return true;
		} else {
			return cadastraNovo(pidctea06);
		}

	}

	private void transfereDadosParaTela() {

		idctea07.setJregger(sicCadnovoView.getNumeroRg());
		idctea07.setJdadnome(sicCadnovoView.getNomePessoa());
		idctea07.setJdadpai(sicCadnovoView.getNomePai());
		idctea07.setJdadmae(sicCadnovoView.getNomeMae());
		datatual = String.valueOf(sicCadnovoView.getDataNasc());
		idctea07.setJdaddia(UtilBean.subStr(datatual, 6, 2));
		idctea07.setJdadmes(UtilBean.subStr(datatual, 4, 2));
		idctea07.setJdadano(UtilBean.subStr(datatual, 0, 4));
		idctea07.setJdadnumpassaporte(sicCadnovoView.getPassaporte());
		idctea07.setJdadpaisorigem(sicCadnovoView.getUf());
		sicCadnovoView.setNumeroPosto((UtilBean.subStr(descel1, 38, 3).trim().equals("")) ? 0 : Integer.parseInt(UtilBean.subStr(descel1, 38, 3)));
		if (!sicCadnovoView.getPassaporte().trim().equals("")) {
			idctea07.setJdadestrangsn("S");
		}
	}

	private boolean alteraCadastroNovo(Idctea07 pidctea07) throws Exception {
		if (pidctea07 == null) {
			transfereDadosParaTela();
			idctea07.setJdadsn("S");
			idctea07.setJtpoperacao("CONFIRMA ALTERACAO (S/N):");
			idctea07.setJregger(sicCadnovoView.getNumeroRg());

			result.include("idctea07", idctea07);
			idctea07.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			sessao.setAtributo("SCRPP002", this);
			return false;
		} else {
			idctea07.receberTela(false, result);
			if (idctea07.temMensagem) {
				result.include("idctea07", idctea07);
				idctea07.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
				sessao.setAtributo("SCRPP002", this);
				return false;
			}
		}

		// Par�grafo: fdr3()
		sicCadnovoView.consultaNumeroRg(idctea07.getJregger());
		if (sicCadnovoView.totalRegistros() == 0) {
			idctea07.setMensagem("REGISTRO INEXISTENTE NO SIC-CADNOVO, FALAR PARA SOLON RG =" + idctea07.getJregger());
			result.include("idctea07", idctea07);
			idctea07.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			sessao.setAtributo("SCRPP002", this);
			return false;
		}

		sicCadnovoView.setTipoRg("ET");
		sicCadnovoView.setNomePessoa(idctea07.getJdadnome());
		sicCadnovoView.setNomePai(idctea07.getJdadpai());
		sicCadnovoView.setNomeMae(idctea07.getJdadmae());
		sicCadnovoView.setDataReg(UtilBean.obtemDataCorrente());
		jDtNasc = Integer.parseInt(idctea07.getJdadano()) * 10000 + Integer.parseInt(idctea07.getJdadmes()) * 100 + Integer.parseInt(idctea07.getJdaddia());
		sicCadnovoView.setDataNasc(jDtNasc);
		sicCadnovoView.setMatriculaFuncAtu(sicCadnovoView.getMatriculaFuncAtu());
		sicCadnovoView.setUf(idctea07.getJdadpaisorigem());
		sicCadnovoView.setHoraAtu(Integer.parseInt((new SimpleDateFormat("HHmmssS").format(Calendar.getInstance().getTime()))));
		sicCadnovoView.setNumeroPosto((UtilBean.subStr(descel1, 38, 3).trim().equals("")) ? 0 : Integer.parseInt(UtilBean.subStr(descel1, 38, 3)));
		if (!idctea07.getJdadnumpassaporte().trim().equals("")) {
			sicCadnovoView.setPassaporte(idctea07.getJdadnumpassaporte());
		}
		sicCadnovoView.atualiza();
		// dao.commit();
		// dao.rollback();
		impAtestaEstrang(idctea07.getJdadestrangsn());

		return true;
	}

	private boolean excluiCadastroNovo(Idctea07 pidctea07) throws JRException, ParseException {
		if (pidctea07 == null) {
			transfereDadosParaTela();
			idctea07.setJdadsn("S");
			idctea07.setJtpoperacao(" CONFIRMA EXCLUSAO (S/N):");
			idctea07.setJregger(sicCadnovoView.getNumeroRg());

			result.include("idctea07", idctea07);
			idctea07.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			sessao.setAtributo("SCRPP002", this);
			return false;
		} else {
			idctea07.receberTela(false, result);
			if (idctea07.temMensagem) {
				result.include("idctea07", idctea07);
				idctea07.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
				sessao.setAtributo("SCRPP002", this);
				return false;
			}
		}

		// Par�grafo: fdr4()
		sicCadnovoView.consultaNumeroRg(idctea07.getJregger());
		if (sicCadnovoView.totalRegistros() == 0) {
			idctea07.setMensagem("REGISTRO INEXISTENTE NO SIC-CADNOVO, FALAR PARA SOLON RG =" + idctea07.getJregger());
			result.include("idctea07", idctea07);
			idctea07.enviarTela(false, result, getClass().getSimpleName().toUpperCase());
			sessao.setAtributo("SCRPP002", this);
			return false;
		}
		if (sicCadnovoView.getTipoRg().trim().equals("ET")) {
			// RELATORIO 1 PS=33 LS=80 SG=OFF
			report.setPageSize(33);
			report.setLineSize(80);
			// RELATORIO
			report.setNovoTipoReg(sicCadnovoView.getTipoRg());
			report.setNovoRegger(sicCadnovoView.getNumeroRg());
			report.gravaRelatorio(4);

			// RELATORIO
			sicCadnovoView.exclui();
			// dao.commit();
			UtilBean.mensagemTexto("REGISTRO DELETADO O.K");
		}

		return true;
	}

	private void estatistica() {
		motida = UtilBean.moveSubstring(motida, 0, 3, "A", "IDC");
		motida = UtilBean.moveSubstring(motida, 3, 2, "N", 99);
		motida = UtilBean.moveSubstring(motida, 5, 8, "N", UtilBean.obtemDataCorrente());
		motida = UtilBean.moveSubstring(motida, 13, 7, "N", idctea06.getJmatriculaj());
		if (vwCadest.consultaMotida("IDC", 99, UtilBean.obtemDataCorrente(), idctea06.getJmatriculaj())) {
			mover();
			vwCadest.atualiza();
			// ADD 1 TO VW-CADEST.DINEST-QUANTI (9)
			// dao.commit();
		}
		if (vwCadest.registrosProcessados() == 0) {
			mover();
			vwCadest.grava();
			// dao.commit();
		}
		// dao.rollback();
		// RESET��� VW-CADEST.DINEST-NUMSEQ
		// RESET��� VW-CADEST.DINEST-QUANTI(7)
		// RESET��� VW-CADEST.DINEST-QUANTI(9)
	}

	private void mover() {
		vwCadest.setCodigoModelo("IDC");
		vwCadest.setCodigoTipo(99);
		vwCadest.setDataAtualiz(UtilBean.obtemDataCorrente());
		vwCadest.setMatriculaFunc(idctea06.getJmatriculaj());
		horatemp = Integer.parseInt((new SimpleDateFormat("HHmmss").format(Calendar.getInstance().getTime())));
		vwCadest.setHoraem(horatemp);
		vwCadest.setNumeroPosto(vwDidcad.getNumeroPosto());
		if (idctea06.getJregger() == 0) {
			vwCadest.setNumeroRg(vwDidcad.getNumeroRg());
		} else {
			vwCadest.setNumeroRg(idctea06.getJregger());
		}
		if (idctea07.getJdadnome().trim().equals("")) {
			vwCadest.setNomeId(vwDidcad.getNomeIdent());
		} else {
			vwCadest.setNomeId(idctea07.getJdadnome());
		}
		vwCadest.setNumeroSeq(vwCadest.getNumeroSeq() + 1);
		vwCadest.setQuantTrans(7, vwCadest.getQuantTrans(7) + 1);
	}

	private void gravaDidEmissao() {
		vwEmissao.setRegGeralEmitido(idctea06.getJregger());
		vwEmissao.setNomeIdent(report.getNomide(1));
		vwEmissao.setTipReg(3);
		vwEmissao.setDataEmissao(UtilBean.obtemDataCorrente());
		horatemp = Integer.parseInt((new SimpleDateFormat("HHmmss").format(Calendar.getInstance().getTime())));
		vwEmissao.setHoraEmissaoCarteira(horatemp);
		vwEmissao.setMatriculaFunc(sessao.getScrg001().getWMatriculaw());
		vwEmissao.setNumpos(vwDidcad.getNumeroPosto());
		vwEmissao.setNumeroSerieRg(vwDidcad.getNumeroSerieCart());
		vwEmissao.setUfeder(vwDidcad.getUfMunicipioExp());
		vwEmissao.grava();
		// dao.commit();
		// dao.rollback();
	}

	private void restaurarEstados(Idctea05 pidctea05, Idctea06 pidctea06, Idctea07 pidctea07, Scrtc001 pscrtc001) {
		this.scrpp002Anterior = (Scrpp002) sessao.getAtributo("SCRPP002");
		if (pidctea05 != null) {
			idctea05 = pidctea05;
			idctea05.setJempresaj(sessao.getScrg001().getWEmpresaw());
			idctea05.setJprogramaj(sessao.getScrg001().getWProgramaw());
			idctea05.setJversaoj(sessao.getScrg001().getWVersaow());
		} else {
			idctea05 = this.scrpp002Anterior.idctea05;
		}
		idctea05.setSessao(sessao);
		if (pidctea06 != null) {
			idctea06 = pidctea06;
			idctea06.setJempresaj(sessao.getScrg001().getWEmpresaw());
			idctea06.setJprogramaj(sessao.getScrg001().getWProgramaw());
			idctea06.setJversaoj(sessao.getScrg001().getWVersaow());
		} else {
			idctea06 = this.scrpp002Anterior.idctea06;
		}
		idctea06.setSessao(sessao);
		if (pidctea07 != null) {
			idctea07 = pidctea07;
			idctea07.setJempresaj(sessao.getScrg001().getWEmpresaw());
			idctea07.setJprogramaj(sessao.getScrg001().getWProgramaw());
			idctea07.setJversaoj(sessao.getScrg001().getWVersaow());
		} else {
			idctea07 = this.scrpp002Anterior.idctea07;
		}
		idctea07.setSessao(sessao);
		if (pscrtc001 != null) {
			scrtc001 = pscrtc001;
			scrtc001.setjEmpresaj(sessao.getScrg001().getWEmpresaw());
			scrtc001.setjProgramaj(sessao.getScrg001().getWProgramaw());
			scrtc001.setjVersaoj(sessao.getScrg001().getWVersaow());
		} else {
			scrtc001 = this.scrpp002Anterior.scrtc001;
		}
		scrtc001.setSessao(sessao);
		this.faseproc = this.scrpp002Anterior.faseproc;
	}

	protected void setMensagem(String mensagem) {
		this.mensagemInicial = mensagem;
	}
}
